<?php
namespace app\user\controller;





use think\Db;
use think\Request;
use app\user\model\So9_order;
use app\user\model\User_shop;
use app\user\model\Finance_tixian;
use app\user\model\Product_cate;
class Seller extends \think\Controller
{

    public function _initialize()
    {

        if (!session('?bid')) {

            $this->redirect('user/login/index');


        }

    }


    public function index()
    {

        return view();


    }


    public function addproduct(Product_cate $Product_cate){

        $s=input('param.');
        $info = $Product_cate->select();
      if(IS_AJAX()){

          if($s['tid']){

              unset($s['id']);
              Db::table('product_1')->where(['id'=>$s['tid']])->update($s);
              $json['code']=200;

          }else{

              $s['addtime']=date('Y-m-d H:i:s');
              $s['bid']=session('bid');
              $s['addname']="商家上传";

               Db::table('product_1')->insert($s);
               $json['code']=200;


          }


        return json($json);

      }else{

          if($s['id']){

            $t=Db::table('product_1')->find($s['id']);
            $this->assign('t',$t);
          }
          $this->assign('info',$info);
          return view();


      }





    }

    public function productlist(){

        $where['bid']=session('bid');
        $list = Db::table('product_1')->where($where)->alias('p')->order('id desc')->paginate(25);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
       
        $this->assign('list', $list);
        $this->assign('page', $page);
       
        return view();


    }



    public function delpro(){

        $bid=session('bid');
        $where['bid']=$bid;
        $where['id']=input('v');
       $flag= Db::table('product_1')->where($where)->delete();
      $json['code']=$flag ? 200  : 400;
      return json($json);


    }


    public function so9order(){
       

       $bid=session('bid');
       $where['bid']=$bid;
       $where['state']=['gt',0];

       $list=Db::table('so9_order')->where($where)->order('id desc')->select();
       $this->assign('list',$list);

      
  
    	return view();
    }
    public function block(So9_order $so9_order)
    {
    	$id = input('post.id');
        $res = $so9_order->where('id', $id)->find();
        if ($res['state'] == "2") {
            $vi = $so9_order->where('id', $id)->update(['state' => '1']);
        } else {
            $vi = $so9_order->where('id', $id)->update(['state' => '2']);
        }
        if ($vi == "2") {
            return json(['status' => false]);
        } else {
            return json(['status' => true]);
        }
    }
    public function getorder()
    {

    }



    public function main()
    {

      $t=Db::table('user_shop')->where(['id'=>session('bid')])->find();
      $this->assign('t',$t);
      return view();



    }


    public function no(So9_order $so9_order,User_shop $user_shop,Finance_tixian $Finance_tixian){
    	$bid = $user_shop->orderinfo();
    	$id  = $so9_order->order();
    	$count = count($id);
		$amount = 0;
		$a = 0.15;
		$info = $Finance_tixian->getinfo();
		if (empty($info)) {
			for ($i=0; $i < $count; $i++) { 
		    
		    	$amount += $id[$i]['money'];
			}
			$b = $amount * $a;
			$result = $amount-$b;
		}else{
			$result = $Finance_tixian->getinfo();
			$result = $result['balance'];
		}

		$this->assign('bid',$bid);
    	$this->assign('result',$result);
        return view();
    }
    public function getinfo()
    {
    	
    }
    public function getgoods(Finance_tixian $Finance_tixian)
    {
    	$too = input();
    	// dump($too);die;
		$res = $Finance_tixian->create($too);
		//dump($res);die;
		if($res){
			//$this->dohandle('提现');
			return json(1);
		}else{
			return json(2);			
		}
    }







    public function meinfo()
	{
	 if (IS_AJAX()) {
            $s = input('param.');
            unset($s['id']);
            $where['ID'] = session('uid');
            $flag = Db::table('user')->where($where)->update($s);
            $json['code'] = $flag ? 200 : 400;
            $json['msg'] = $flag ? "资料修改成功" : "资料修改失败，请重试";

            return json($json);


        } else {


            $uid = session('uid');
            $user = Db::table('user')->find($uid);
            $this->assign('t', $user);
            $this->assign('address', $address);
            return view();


        }

	}

	public function password()
	{
		  if(IS_AJAX()){


            $s = input('param.');
            if (!$s['pass'] || $s['password'] != $s['word']) {
                $json['msg'] = "请输入正确的密码";
                $json['code'] = 400;
            } else {

                $where['PassWord'] = md5($s['pass']);
                $where['ID'] = session('uid');

                $user = Db::table('user')->where($where)->find();


                if ($user) {
                    $json['code'] = 200;

                    $update['PassWord'] = md5($s['password']);
                    Db::table('user')->where(['ID' => session('uid')])->update($update);

                    $json['code'] = 200;

                    $json['url'] = url('user/buyers/index');

                     } else {

                    $json['msg'] = "您的旧密码错误";
                    $json['code'] = 400;
              }


                return json($json);

                }
            }

            return  view();
        }
          

	
 public function out(){



      session('bid',null);
      $this->redirect('login/index');
 }



public function changepwd(){

	
	

       if(IS_AJAX()){
     
    $s=input('param.');
    if(!$s['p1'] || $s['p2']!=$s['p3']){

        $json['msg']="资料输入不正确";
        $json['code']=400;

    }else{


        $u=Db::table('user_shop')->find(session('bid'));

        $where['ID']=$u['uid'];
        $where['pwd']=md5($s['p1']);
        $user=Db::table('user')->where($where)->find();
        if($user){
             $data['pwd']=md5($s['p2']);
             Db::table('user')->where($where)->update($data);
             $json['code']=200;

        }else{
          

            $json['code']=400;
            $json['msg']="您输入的旧密码错误";

        }

    } 
    
   return json($json);

    }else{

      return view();   
    }



}



	public function mehemiao()
	{

		return view();
	}


	public function info()
	{

		return view();
	}


	public function gps()
	{

		return view();
	}



	public function issueprice()
	{

		return view();
	}


	public function shop()
	{

		return view();
	}

	public function issueshop()
	{

		return view();
	}


	public function supply()
	{

		return view();
	}


	public function issuesupply()
	{

		return view();
	}


	public function meorder()
	{

		return view();
	}


	public function bank()
	{

		return view();
	}


	public function ordermoney()
	{

		return view();
	}


	public function money()
	{

		return view();
	}


	public function name()
	{

		return view();
	}


	public function certificate()
	{
		return view();
	}


	public function blocshop()
	{

		return view();
	}


	public function customize()
	{

		return view();
	}


	public function hire()
	{

		return view();
	}


	public function shopinfo()
	{

		return view();
	}


	public function product()
	{


		return view();
	}


	public function visit()
	{
		return view();
	}

	public function manor()
	{

		return view();
	}


	public function manorinfo()
	{

		return view();
	}


	public function manorliuliujie()
	{

		return view();
	}

	public function liuliujie()
	{
		return view();
	}

	public function blocinfo()
	{

		return view();
	}

	public function news()
	{
		return view();
	}


	public function apply()
	{
		if(IS_AJAX()){

			$s=input('param.');
			 if(!$s['Name'] || !$s['Contact'] || !$s['QQ']|| $s['Email']|| !$s['WebUrl']|| !$s['AdressDis']|| !$s['Content']) {

			 	$json['code']=400;
			 	$json['msg']="请输入正确信息";
			 }
			 $where['AccountID']=session('uid');
			 $where['Name']=$s['Name'];
			 $where['Contact']=$s['Contact'];
			 $where['QQ']=$s['QQ'];
			 $where['Email']=$s['Email'];
			 $where['WebUrl']=$s['WebUrl'];
			 $where['AdressDis']=$s['AdressDis'];
			 $where['Content']=$s['Content'];
			 $add=Db::table('service_logistics')->insert($where);
			 if($add){
			 	$json['code']=200;
			 	$json['msg']="添加成功";


			 }else{

			 	$json['code']=400;
			 	$json['msg']="请输入正确信息";

			 }
			 return json($json);




		}
		return view();
	}

	public function car() 
	{


		$where['AccountID']=session('uid');

		dump($where);

		$car=Db::table('service_logistics')->alias('l')							
										   ->join('service_logistics_car c','l.AccountID = c.LogisticsID')
										   ->field('c.Name as cname,c.CarNum,c.Contact,c.Mobile as 											cMobile,c.CarLength,c.CarWidth,c.CarHigh,l.AccountID')
											// ->where($where)
											->select();
											dump($car);

		// $where['LogisticsID']=session('uid');
		// $car=Db::table('service_logistics_car')->where($where)->select();

		 $this->assign('car',$car);

		return view();
	}

	public function addcar(){
		if(IS_AJAX()){

			$s=input('param.');
			 if(!$s['Name'] || !$s['CarLength'] || !$s['CarLoad']|| $s['CarNum']|| !$s['Contact']|| $s['Mobile']!=11|| !$s['Content']) {

			 	$json['code']=400;
			 	$json['msg']="请输入正确信息";
			 }

			 $where['LogisticsID']=session('uid');
			 $where['Name']=$s['Name'];
			 $where['CarLength']=$s['CarLength'];
			 $where['CarLoad']=$s['CarLoad'];
			 $where['CarNum']=$s['CarNum'];
			 $where['Contact']=$s['Contact'];
			 $where['Mobile']=$s['Mobile'];
			 $where['Content']=$s['Content'];
			 $add=Db::table('service_logistics_car')->insert($where);
			 if($add){
			 	$json['code']=200;
			 	$json['msg']="添加成功";


			 }else{

			 	$json['code']=400;
			 	$json['msg']="请输入正确信息";

			 }
			 return json($json);

		}
		$where['LogisticsID']=session('uid');
		$car=Db::table('service_logistics_car')->alias('c')
											   ->join('unit u','c.LoadUnit = u.unid')
											   ->where($where)
											   ->select();
										

		$this->assign('car',$car);

		return view();
	}

	public function del(){


      if(IS_AJAX()){

      $s=input("param.");

      $where['ID']=$s['ID'];

      $del=Db::table('service_logistics_car')->where($where)->delete();

      $json["code"]=$del? 200: 400;

      $json["msg"]=$del?"删除成功":"删除失败";

        return json($json);
      }
    }

    public function savecar(){
    	$s=input('param.');
    	$where['id']=$s['id'];
    		$where['LogisticsID']=session('uid');

            $data=Db::table('service_logistics_car')->where($where)->find();

            $this->assign('t',$data);

            return view();


    }


      public function save(){
            if(IS_AJAX()){
              $s=input('param.');
			 $where['Name']=$s['Name'];
			 $where['CarLength']=$s['CarLength'];
			 $where['CarLoad']=$s['CarLoad'];
			 $where['CarNum']=$s['CarNum'];
			 $where['Contact']=$s['Contact'];
			 $where['Mobile']=$s['Mobile'];
			 $where['Content']=$s['Content'];
              $flag=Db::table('service_logistics_car')->where('ID',$s['ID'])->update($where);
             
                 $json['code']=$flag? 200 : 400;
                 $json['msg']=$flag? "修改成功" :"修改失败";

               return json($json);

            }

             
          }







	public function road()
	{
		//$where['LogisticsCarsID']=session('uid');
		$data=Db::table('service_logistics_line')->alias('a')
												 ->field('c.Name,a.BeginPlace,a.EndPlace')
												 ->join('service_logistics_car c','c.ID = a.LogisticsCarsID')
												 ->where($where)
												 ->select();
		$this->assign('data',$data);
		return view();
	}

	public function goodslist()
	{

		return view();
	}

	public function estimate()
	{
		return view();
	}

	public function emit()
	{
		return view();
	}


	public function complain()
	{
		return view();
	}

	public function refund()
	{
		return view();
	}

	public function feedback()
	{
		return view();
	}
	//商户查看提现列表展示
	public function checkthe(Finance_tixian $Finance_tixian)
	{
		$info = $Finance_tixian->showinfo();
		//dump($info);die;
		$this->assign('info',$info);
		return $this->fetch();
	}
	//提现状态的处理
	public function checkthelist(Finance_tixian $Finance_tixian)
	{
		$id = input('post.id');
		// dump($id);die;
		 $res = $Finance_tixian->where('id', $id)->find();

        if (!$res['state'] == "4") {
            $vi = $Finance_tixian->where('id', $id)->update(['state' => '4']);
        } else {
            $vi = $Finance_tixian->where('id', $id)->update(['state' => '5']);
        }
        if ($vi == "4") {
            return json(['status' => false]);
        } else {
            return json(['status' => true]);
        }
	}
}
